home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™ 1987-1994 / MacHack™ '87 / Source ƒ.sea / Source ƒ / emacs source ƒ / CRYPT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-28  |  6.8 KB  |  202 lines  |  [TEXT/MARC]

  1. /*    Crypt:    Encryption routines for MicroEMACS
  2.         written by Dana Hoggatt and Daniel Lawrence
  3. */
  4.  
  5. #include    <stdio.h>
  6. #include    "estruct.h"
  7. #include    "edef.h"
  8.  
  9. #if    CRYPT
  10. setkey(f, n)    /* reset encryption key of current buffer */
  11.  
  12. int f;        /* default flag */
  13. int n;        /* numeric argument */
  14.  
  15. {
  16.     register int status;    /* return status */
  17.     char key[NPAT];        /* new encryption string */
  18.  
  19.     /* get the string to use as an encrytion string */
  20.         if ((status = mlreply("Encryption String: ", key, NPAT - 1)) != TRUE)
  21.                 return(status);
  22.  
  23.     /* and encrypt it */
  24.     crypt((char *)NULL, 0);
  25.     crypt(key, strlen(key));
  26.  
  27.     /* and save it off */
  28.     strcpy(curbp->b_key, key);
  29.     mlwrite(" ");        /* clear it off the bottom line */
  30.     return(TRUE);
  31. }
  32.  
  33. /**********
  34.  *
  35.  *    crypt - in place encryption/decryption of a buffer
  36.  *
  37.  *    (C) Copyright 1986, Dana L. Hoggatt
  38.  *    1216, Beck Lane, Lafayette, IN
  39.  *
  40.  *    When consulting directly with the author of this routine, 
  41.  *    please refer to this routine as the "DLH-POLY-86-B CIPHER".  
  42.  *
  43.  *    This routine was written for Dan Lawrence, for use in V3.8 of
  44.  *    MICRO-emacs, a public domain text/program editor.  
  45.  *
  46.  *    I kept the following goals in mind when preparing this function:
  47.  *
  48.  *        1.    All printable characters were to be encrypted back
  49.  *        into the printable range, control characters and
  50.  *        high-bit characters were to remain unaffected.  this
  51.  *        way, encrypted would still be just as cheap to 
  52.  *        transmit down a 7-bit data path as they were before.
  53.  *
  54.  *        2.    The encryption had to be portable.  The encrypted 
  55.  *        file from one computer should be able to be decrypted 
  56.  *        on another computer.
  57.  *
  58.  *        3.    The encryption had to be inexpensive, both in terms
  59.  *        of speed and space.
  60.  *
  61.  *        4.    The system needed to be secure against all but the
  62.  *        most determined of attackers.
  63.  *
  64.  *    For encryption of a block of data, one calls crypt passing 
  65.  *    a pointer to the data block and its length. The data block is 
  66.  *    encrypted in place, that is, the encrypted output overwrites 
  67.  *    the input.  Decryption is totally isomorphic, and is performed 
  68.  *    in the same manner by the same routine.  
  69.  *
  70.  *    Before using this routine for encrypting data, you are expected 
  71.  *    to specify an encryption key.  This key is an arbitrary string,
  72.  *    to be supplied by the user.  To set the key takes two calls to 
  73.  *    crypt().  First, you call 
  74.  *
  75.  *        crypt(NULL, vector)
  76.  *
  77.  *    This resets all internal control information.  Typically (and 
  78.  *    specifically in the case on MICRO-emacs) you would use a "vector" 
  79.  *    of 0.  Other values can be used to customize your editor to be 
  80.  *    "incompatable" with the normally distributed version.  For 
  81.  *    this purpose, the best results will be obtained by avoiding
  82.  *    multiples of 95.
  83.  *
  84.  *    Then, you "encrypt" your password by calling 
  85.  *
  86.  *        crypt(pass, strlen(pass))
  87.  *
  88.  *    where "pass" is your password string.  Crypt() will destroy 
  89.  *    the original copy of the password (it becomes encrypted), 
  90.  *    which is good.  You do not want someone on a multiuser system 
  91.  *    to peruse your memory space and bump into your password.  
  92.  *    Still, it is a better idea to erase the password buffer to 
  93.  *    defeat memory perusal by a more technical snooper.  
  94.  *
  95.  *    For the interest of cryptologists, at the heart of this 
  96.  *    function is a Beaufort Cipher.  The cipher alphabet is the 
  97.  *    range of printable characters (' ' to '~'), all "control" 
  98.  *    and "high-bit" characters are left unaltered.
  99.  *
  100.  *    The key is a variant autokey, derived from a wieghted sum 
  101.  *    of all the previous clear text and cipher text.  A counter 
  102.  *    is used as salt to obiterate any simple cyclic behavior 
  103.  *    from the clear text, and key feedback is used to assure 
  104.  *    that the entire message is based on the original key, 
  105.  *    preventing attacks on the last part of the message as if 
  106.  *    it were a pure autokey system.
  107.  *
  108.  *    Overall security of encrypted data depends upon three 
  109.  *    factors:  the fundamental cryptographic system must be 
  110.  *    difficult to compromise; exhaustive searching of the key 
  111.  *    space must be computationally expensive; keys and plaintext 
  112.  *    must remain out of sight.  This system satisfies this set
  113.  *    of conditions to within the degree desired for Micro-EMACS.
  114.  *
  115.  *    Though direct methods of attack (against systems such as 
  116.  *    this) do exist, they are not well known and will consume 
  117.  *    considerable amounts of computing time.  An exhaustive
  118.  *    search requires over a billion investigations, on average.
  119.  *
  120.  *    The choice, entry, storage, manipulation, alteration, 
  121.  *    protection and security of the keys themselves are the 
  122.  *    responsiblity of the user.  
  123.  *
  124.  **********/
  125.  
  126. crypt(bptr, len)
  127. register char *bptr;    /* buffer of characters to be encrypted */
  128. register unsigned len;    /* number of characters in the buffer */
  129. {
  130.     register int cc;    /* current character being considered */
  131.  
  132.     static long key = 0;    /* 29 bit encipherment key */
  133.     static int salt = 0;    /* salt to spice up key with */
  134.  
  135.     if (!bptr) {        /* is there anything here to encrypt? */
  136.         key = len;    /* set the new key */
  137.         salt = len;    /* set the new salt */
  138.         return;
  139.     }
  140.     while (len--) {        /* for every character in the buffer */
  141.  
  142.         cc = *bptr;    /* get a character out of the buffer */
  143.  
  144.         /* only encipher printable characters */
  145.         if ((cc >= ' ') && (cc <= '~')) {
  146.  
  147. /**  If the upper bit (bit 29) is set, feed it back into the key.  This 
  148.     assures us that the starting key affects the entire message.  **/
  149.  
  150.             key &= 0x1FFFFFFFL;    /* strip off overflow */
  151.             if (key & 0x10000000L) {
  152.                 key ^= 0x0040A001L;    /* feedback */
  153.             }
  154.  
  155. /**  Down-bias the character, perform a Beaufort encipherment, and 
  156.     up-bias the character again.  We want key to be positive 
  157.     so that the left shift here will be more portable and the 
  158.     mod95() faster   **/
  159.  
  160.             cc = mod95((int)(key % 95) - (cc - ' ')) + ' ';
  161.  
  162. /**  the salt will spice up the key a little bit, helping to obscure 
  163.     any patterns in the clear text, particularly when all the 
  164.     characters (or long sequences of them) are the same.  We do 
  165.     not want the salt to go negative, or it will affect the key 
  166.     too radically.  It is always a good idea to chop off cyclics 
  167.     to prime values.  **/
  168.  
  169.             if (++salt >= 20857) {    /* prime modulus */
  170.                 salt = 0;
  171.             }
  172.  
  173. /**  our autokey (a special case of the running key) is being 
  174.     generated by a wieghted checksum of clear text, cipher 
  175.     text, and salt.   **/
  176.  
  177.             key = key + key + cc + *bptr + salt;
  178.         }
  179.         *bptr++ = cc;    /* put character back into buffer */
  180.     }
  181. }
  182.  
  183. static int mod95(val)
  184. register int val;
  185. {
  186.     /*  The mathematical MOD does not match the computer MOD  */
  187.  
  188.     /*  Yes, what I do here may look strange, but it gets the
  189.         job done, and portably at that.  */
  190.  
  191.     while (val >= 9500)
  192.         val -= 9500;
  193.     while (val >= 950)
  194.         val -= 950;
  195.     while (val >= 95)
  196.         val -= 95;
  197.     while (val < 0)
  198.         val += 95;
  199.     return (val);
  200. }
  201. #endif
  202.